<?php
require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'init.inc.php';

class ExampleQueries extends PHPUnit_Framework_TestCase {

	var $parser;
	
	
	function __construct()
    {
        $this->parser = ARC2::getSPARQLParser();
		$this->make_query_array();
	}
	
	function makeQuery($query,$silent=false)  {
		$this->parser->parse($query);
		$errors = $this->parser->getErrors();
		if ( $errors ) {
			if ( $silent ) {
				return false;
			} else {
				$this->assertEquals( !$errors, true, "cannot parse query [[$query]]: " . print_r($errors,true) );
			}
		}
		return $this->parser->getQueryInfos();
	}
	
	function makeSimpleQuery() {
		return $this->makeQuery($this->simpleQuery);
	}
	
	function makeComplicatedButUselessQuery() {
		return $this->makeQuery($this->complicatedButUselessQuery);
	}
	
	function makeTypicalQueryNeedingToKnowClassLessRestrictiveThanPredicate() {
		return $this->makeQuery($this->typicalQueryNeedingToKnowClassLessRestrictiveThanPredicate);
	}
	
	var $complicatedButUselessQuery = "PREFIX aiiso: <http://purl.org/vocab/aiiso/schema#>
	PREFIX resource: <http://purl.org/vocab/resourcelist/schema#>
	PREFIX sioc: <http://rdfs.org/sioc/spec/>
	PREFIX eg: <http://example.com/>

	select distinct ?pred ?s
	where
	{
	  ?s a resource:List .
	   optional { ?s eg:pred1 ?z } .
	   filter( bound(?z) && ( ?z<10 || ?z=<http://example.com/> ) ) .
	    ?s ?pred ?obj ; ?p2 'fred'

	}
	order by ?s desc(?obj)
	limit 5
	offset 5
	";
	
	var $simpleQuery = "PREFIX eg: <http://example.com/>
	select ?a ?b ?c
	where
	{
	  ?s ?p ?o;
	     eg:hasParent ?o2.
	  ?o2 eg:hasName 'fred'
	}";

    var $typicalQueryNeedingToKnowClassLessRestrictiveThanPredicate = "select ?subjectUri ?subjectName
where {
 ?subjectUri a <http://www.w3.org/2004/02/skos/core#Concept> .
 ?subjectUri <http://www.w3.org/2004/02/skos/core#prefLabel> ?subjectName .
 ?subjectUri <http://www.w3.org/2004/02/skos/core#inScheme> <http://talisaspire.com/subjects/> .
}
order by ?subjectName";

	var $pp_typicalQueryNeedingToKnowClassLessRestrictiveThanPredicate = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

select ?subjectUri ?subjectName 
where {
 ?subjectUri a <http://www.w3.org/2004/02/skos/core#Concept> .
 ?subjectUri <http://www.w3.org/2004/02/skos/core#prefLabel> ?subjectName .
 ?subjectUri <http://www.w3.org/2004/02/skos/core#inScheme> <http://talisaspire.com/subjects/>
}
order by ?subjectName

";

	var $optimisedTypicalQueryNeedingToKnowClassLessRestrictiveThanPredicate = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

select ?subjectUri ?subjectName 
where {
 ?subjectUri <http://www.w3.org/2004/02/skos/core#inScheme> <http://talisaspire.com/subjects/> .
 ?subjectUri a <http://www.w3.org/2004/02/skos/core#Concept> .
 ?subjectUri <http://www.w3.org/2004/02/skos/core#prefLabel> ?subjectName
}
order by ?subjectName

";

	// orogonal query started "select distinct(?resource) ..." but this is extended SPARQL syntax
	// and ARC2 can not parse
	var $real_query_1 = "PREFIX aspire: <http://talisaspire.com/schema#>
PREFIX bibo: <http://purl.org/ontology/bibo/>

select distinct ?resource
where
{
  ?resource a bibo:Book .
  ?resource aspire:listReferences ?lists .
  ?lists aspire:list <http://resourcelists.st-andrews.ac.uk/lists/34B725C4-2D05-64F4-3FD6-5C8DBF95DE3C> .
}";


	//To make it work I just restructured the query to this, which executes in under a second, because the very first clause restricts the result set down to a tiny number:

	var $opt_real_query_1 = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX aspire: <http://talisaspire.com/schema#>
PREFIX bibo: <http://purl.org/ontology/bibo/>

select distinct ?resource 
where {
 ?lists aspire:list <http://resourcelists.st-andrews.ac.uk/lists/34B725C4-2D05-64F4-3FD6-5C8DBF95DE3C> .
 ?resource aspire:listReferences ?lists .
 ?resource a bibo:Book
}


";


// Current version of the query, that times out:

	var $real_query_2 = "DESCRIBE ?listUri
WHERE
{
  ?listUri <http://purl.org/vocab/resourcelist/schema#contains> ?itemUri .
  ?itemUri <http://purl.org/vocab/resourcelist/schema#resource> <http://www.readinglists.manchester.ac.uk/resources/AB7CEDEF-066A-2027-C176-1AC33C7D40C5> .
}";

// heres a new version of the same query which always works:

	var $opt_real_query_2 = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

describe ?listUri 
where {
 ?itemUri <http://purl.org/vocab/resourcelist/schema#resource> <http://www.readinglists.manchester.ac.uk/resources/AB7CEDEF-066A-2027-C176-1AC33C7D40C5> .
 ?listUri <http://purl.org/vocab/resourcelist/schema#contains> ?itemUri
}
";
	function make_query_array() {
		$this->real_queries = array(
			array($this->real_query_1,$this->opt_real_query_1,"real query 1"),
			array($this->real_query_2,$this->opt_real_query_2,"real query 2: describe")
		);
	}

	var $real_queries;

	
}


?>